﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using System.Data.Common;

using VitCode.Castle.DataAccess;
using VitCode.Castle.Entities;
using VitCode.Castle.Helper;

namespace AMS.Web.BackStageManagement
{
    public partial class UserGroupUpdateAll : Page
    {
        public string strWhere = "", strSql = "", strLink = "", strMsg = "";
        string MenuCode = "";

        CastleAccess acc = new CastleAccess();



        protected void Page_Load(object sender, EventArgs e)
        {



            if (!IsPostBack)
            {
               ShowTree();
            }
        }

        public void ShowTree()
        {
            TreeView_Group.Nodes.Clear();

            TreeNode node_0 = new TreeNode();
            node_0.ImageUrl = "../../Images/calendar.gif";
            node_0.Text = "角色列表" + "<a herf='#'></a>";
            node_0.Value = "0";

         

            node_0.SelectAction = TreeNodeSelectAction.Expand;
            node_0.Expanded = true;
            this.TreeView_Group.Nodes.Add(node_0);
            this.ViewState["ds"] = acc.GetDataSetBySql("select * from Frame_Role order by Alias");

            DataSet ds = (DataSet)this.ViewState["ds"];

            DataView dvTree = new DataView(ds.Tables[0]);


            foreach (DataRowView Row in dvTree)
            {
                TreeNode Node = new TreeNode();
                Node.Text = Row["Name"].ToString().Trim();
                Node.Value = Row["Id"].ToString().Trim();

                Node.ImageUrl = "../../Images/notes.gif";
                Node.Text += "[" + Row["Alias"] + "]";
 
                node_0.ChildNodes.Add(Node);
                Node.Expanded = true;

            }

        }


 

        /* 得到所选择的节点=========================================================
         * 调用方法 string MenuCodeAll = GetTreeCheck(TreeView_Menu.Nodes[0]);
         * 
         */
        string FunctionString = "";
        public string GetTreeCheck(TreeNode tn)
        {
            foreach (TreeNode treenode in tn.ChildNodes)
            {
                if (treenode.Checked)
                {
                    string va = treenode.Value;
                    string vb = treenode.Text;
                    FunctionString += ", " + va + "";
                }
                else
                {
                }

                GetTreeCheck(treenode);//调用自身函数循环遍历TreeView

            }
            return FunctionString;//返回FunctionString字符串
        }

        protected void Button_Cancel_Click(object sender, EventArgs e)
        {
            
            Response.Redirect("UserList.aspx", false);
        }

        /// <summary>
        /// 批量设置用户的角色
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button_Update_Group_Click(object sender, EventArgs e)
        {
            string strUserName = Request.Cookies["sCheck"].Value.Trim();
            string StrRoles = GetTreeCheck(TreeView_Group.Nodes[0]);

            string[] paraUserName = strUserName.Split(',');
            string[] paraRoles = StrRoles.Split(',');

        
            try
            {
                acc.BeginTransaction();
                for (int i = 0; i < paraUserName.Length; i++)
                {
                    if (paraUserName[i].ToString().Trim() != "")
                    {

                        UserRoleRelation relation = new UserRoleRelation();

                        IList objRelation = acc.GetObjectList(new UserRoleRelation().GetType().Name, "UserId",
                                                              paraUserName[i].ToString().Trim());
                        if (objRelation != null && objRelation.Count > 0)
                        {   
                            for(int j = 0;j<objRelation.Count;j++)
                            {
                                relation = (UserRoleRelation)objRelation[j];
                                acc.Delete(relation);
                            }
                            
                        }


                        for (int l = 0; l < paraRoles.Length; l++)
                        {

                            if (paraRoles[l].ToString().Trim() != "")
                            {
                                UserRoleRelation objUserRoleRelation = new UserRoleRelation();
                                Guid userId = new Guid(paraUserName[i].ToString().Trim());
                                Guid roleId = new Guid(paraRoles[l].ToString().Trim());
                                objUserRoleRelation.UserId = userId;
                                objUserRoleRelation.RoleId = roleId;
                                acc.SaveOrUpdate(objUserRoleRelation);
                                
                            }
                        }


                    }
                }
                acc.Commit();
                Alert.MessageBox("设置成功!");
            }
            catch (Exception)
            {
                acc.RollBack();
                throw;
            }
            finally
            {
                acc.CloseSession();
            }
        }
     
    }
}
